home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Developer Toolbox 6.1
/
SGI Developer Toolbox 6.1 - Disc 4.iso
/
public
/
RTF
/
search_path.C
< prev
next >
Wrap
C/C++ Source or Header
|
1994-08-01
|
3KB
|
135 lines
// search_path.C -
// Copyright (c) 1990, 1991, 1992 Convex Computer Corporation
// All rights reserved.
/**********************************************************************
** File: search_path.C
** Ident: $CHeader: search_path.C 1.1 92/06/11 18:44:50 $
** Language: C++
** Purpose:
** Types
None
** Constants:
** Global Data:
** Static Data:
** Global Functions:
** Public methods:
** Private methods:
** Static Public methods:
** Static Private methods:
** Notes:
** Revision History:
** Who Date Description
--- -------- ----------------------------------------------------
DWC 1-31-92 initial implementation
**********************************************************************/
#include "search_path.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "debug.h"
char SearchPath::buffer[max_path_len+1];
int SearchPath::len;
void
SearchPath::grow(int tot)
{
assert(tot>0);
if(tot >= allocated){
allocated = allocated * 3/2 + 10;
Filename* n = new char*[allocated];
bzero(n, allocated*sizeof(Filename)); //@@ is this redundant?
if(dirs){
bcopy(dirs, n, dir_qty * sizeof(Filename));
current = n + (current - dirs);
delete [] dirs;
}
dirs = n;
}
}
int
SearchPath::add_directories(Path path){
if(!path || !*path)
return dir_qty;
register const char *s, *e;
register char *d;
int len;
for(s = path; s && *s; s = e ? e+1 : 0){
e = strchr(s, ':');
len = e ? e-s : strlen(s);
grow(dir_qty+1);
d = new char[len+1];
strncpy(d, s, len);
d[len] = 0;
dirs[dir_qty++] = (Filename)d;
}
}
SearchPath::Filename SearchPath::find(Filename f, int mode, const char* ext)
{
if(strlen(f) > max_name_len)
return 0;
Filename d, e, ee;
int l;
for(d=first_dir(); d; d=next_dir()){
for(e = ext ? ext : ""; e; (e=ee) && e++){
(ee = strchr(e, ':')) ? (l=ee-e) : (l=strlen(e));
if( (len = strlen(d) + strlen(f) + l) > max_path_len)
continue;
sprintf(buffer, "%s/%s%.*s", d, f, l, e);
if(0 == access(buffer, mode)){
debug(("found %s at %s\n", f, buffer));
return buffer;
}
}
}
return 0;
}
char*
SearchPath::lookup(Filename f, int mode, const char* ext)
{
if(find(f,mode, ext)){
char * ret = new char[len+1];
strcpy(ret, buffer);
return ret;
}else
return 0;
}
FILE*
SearchPath::open(Filename f, const char* ext)
{
if(find(f, R_OK, ext))
return fopen(buffer, "r");
else
return 0;
}